package com.example.test.controller;

import com.example.test.model.Course;
import com.example.test.service.CourseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("course")
public class CourseController extends BaseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping("create")
    private String create(Course course) {
        courseService.create(course);
        return "redirect:/course/queryAll/1";
    }

    @GetMapping("queryAll/{currentPage}")
    private String queryAll(@PathVariable("currentPage") int currentPage) {
        session.setAttribute("pagination", courseService.queryAll(currentPage));
        return "redirect:/course/list.jsp";
    }

    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable("id") int id) {
        session.setAttribute("course", courseService.queryById(id));
        return "redirect:/course/edit.jsp";
    }

    @PostMapping("modify")
    private String modify(Course course) {
        courseService.modify(course);
        return "redirect:/course/queryAll";
    }


}
